package com.ty.paypal.service.plan;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.paypal.http.HttpResponse;
import com.paypal.orders.Money;
import com.ty.paypal.core.Plan;
import com.ty.paypal.core.d.BillingCycles;
import com.ty.paypal.core.d.Frequency;
import com.ty.paypal.core.d.PaymentPreferences;
import com.ty.paypal.core.d.PricingScheme;
import com.ty.paypal.core.d.Taxes;
import com.ty.paypal.core.request.PlanCreateRequest;
import com.ty.paypal.core.request.body.PlanRequest;
import com.ty.paypal.service.PayPalClient;

/**
* @author ty
* @createDate 创建时间：2020年2月22日 下午2:55:30
**/
public class CreatePlans extends PayPalClient{
	
	public HttpResponse<Plan> billingPlans(String productId) throws IOException {
		PlanCreateRequest request = new PlanCreateRequest();
		request.requestBody(buildRequestBody(productId));
		HttpResponse<Plan> response = client().execute(request);
		System.out.println(response.result());
		return response;
	}
	
  public PlanRequest buildRequestBody(String productId) {
	  PlanRequest body = new PlanRequest();
	  body.setProductId(productId);
	  body.setName("共享计划套餐");
	  body.setDescription("这是一个按月、按周付费套餐");
	  
	  List<BillingCycles> billingCycles = new ArrayList<BillingCycles>();
	  billingCycles.add(BillingCycles.builder()
	  	.frequency(Frequency.builder().intervalUnit("MONTH").intervalCount(1).build())
	  	.tenureType("TRIAL")
	  	.sequence(1)
	  	.totalCycles(1L).build());
	  body.setBillingCycles(billingCycles );
	  billingCycles.add(BillingCycles.builder()
			  .pricingScheme(PricingScheme.builder().fixedPrice(new Money().currencyCode("USD").value("10")).build())
			  .frequency(Frequency.builder().intervalUnit("MONTH").intervalCount(1).build())
			  .tenureType("REGULAR")
			  .sequence(2)
			  .totalCycles(12L).build());
	  body.setBillingCycles(billingCycles );
	  
	  body.setPaymentPreferences(PaymentPreferences.builder().autoBillOutstanding(true)
			  .setupFee(new Money().currencyCode("USD").value("10"))
			  .setupFeeFailureAction("CONTINUE")
			  .paymentFailureThreshold(3).build());
	  
	  body.setTaxes(new Taxes("10", false));
	  return body;
  }
  
  public static void main(String[] args) {
	try {
//		System.out.println(new CreatePlans().authorizationString());
		
		new CreatePlans().billingPlans("PROD-7VH55633LS770121F");
	} catch (Exception e) {
		e.printStackTrace();
	}
}
}
